/**
 * Copyrights Christophe Cornu 2010, All rights reserved.
 */
package com.cctl.speaker;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Book {

	public void parseFile(String path, Map<String, String> dictionary,
			Set<Sentence> result) {
		try {
		FileReader fr = new FileReader(path);
		BufferedReader br = new BufferedReader(fr);
		String buffer = "";
		while (true) {
			String line = br.readLine();
			if (line == null)
				break;
			buffer += " " + line;
			if (buffer.length() > 10000) {
				parse(buffer, dictionary, result);
				buffer = "";
			}
		}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void parse(String input, Map<String, String> dictionary,
			Set<Sentence> result) {

		int sentenceStartIndex = -1;
		int wordStartIndex = -1;
		List<String> words = new ArrayList<String>();
		char[] letters = input.toCharArray();
		for (int i = 0; i < letters.length; i++) {
			char letter = letters[i];
			if (sentenceStartIndex == -1) {
				if (isWordSeparator(letter) || isSentenceEnding(letter))
					continue;
				sentenceStartIndex = i;
				wordStartIndex = i;
				continue;
			}
			if (wordStartIndex == -1) {
				if (isWordSeparator(letter))
					continue;
				if (!isSentenceEnding(letter)) {
					wordStartIndex = i;
					continue;
				}
			}
			if (isWordSeparator(letter)) {
				String newWord = new String(letters, wordStartIndex, i - wordStartIndex);
				words.add(newWord);
				wordStartIndex = -1;
				continue;
			}
			if (isSentenceEnding(letter)) {
				if (wordStartIndex != -1) {
					String lastWord = new String(letters, wordStartIndex, i - wordStartIndex);
					words.add(lastWord);
				}
				if (words.size() > 0) {
					result.add(new Sentence(words.toArray(new String[words
							.size()]), dictionary, true));
					sentenceStartIndex = -1;
					wordStartIndex = -1;
					words = new ArrayList<String>();
				}
			}
		}
	}

	boolean isWordSeparator(char letter) {
		return letter == ' ';
	}

	boolean isSentenceEnding(char letter) {
		return letter == '.' || letter == ',' || letter == ':' || letter == '-'
				|| letter == '"' || letter == ';' || letter == '!' || letter == '?';
	}

	public static void main(String[] args) {
		String input = "It is very important, you see, to understand the meaning. You need to break things at the right time - not too early, not too late.";
		String path = "D:/chrix/work/RTC-ExpressC-Full-1.0.1-Win/workspace/Speaker/src/17489-8.txt";
		Book book = new Book();
		Map<String, String> dictionary = new HashMap<String, String>();
		Set<Sentence> result = new HashSet<Sentence>();
		book.parseFile(path, dictionary, result);
		for (Sentence sentence : result)
			System.out.println(sentence);
		System.out.println(result.size()+" sentences, "+dictionary.size()+" words");
	}
}
